import {ModalForm, ProFormRadio, ProFormSelect, ProFormText, ProFormTextArea} from '@ant-design/pro-components';
import React from 'react';

export type CreateOrUpdateFormProps = {
  onCancel: (flag?: boolean, formVals?: API.AlarmRule) => void;
  onSubmit: (values: API.AlarmRule) => Promise<boolean>;
  updateModalOpen: boolean;
  values?: Partial<API.AlarmRule>;
  title?:string;
};

const CreateOrUpdateForm: React.FC<CreateOrUpdateFormProps> = (props) => {

  return (<ModalForm
      title={props?.title}
      layout={"horizontal"}
      labelAlign={"right"}
      labelCol={{span: 6}}
      width="700px"
      open={props.updateModalOpen}
      onFinish={props.onSubmit}
      modalProps={{destroyOnClose: true, onCancel: () => props.onCancel()}}
    >
      <ProFormText initialValue={props?.values?.id} hidden={true} name='id'/>
      <ProFormSelect
        initialValue={props.values?.monitorName || 'DELAYTIME'}
        rules={[
          {
            required: true,
            message: '监控项目不能为空',
          },
        ]}
        options={[
          {
            label:'延迟',
            value:'DELAYTIME'
          },
          {
            label:'异常',
            value:'EXCEPTION'
          },
          {
            label:'Pipeline超时',
            value:'PIPELINETIMEOUT'
          },
          {
            label:'Process超时',
            value:'PROCESSTIMEOUT'
          },
          {
            label:'position超时',
            value:'POSITIONTIMEOUT'
          },
        ]}
        width="md"
        label="监控项目"
        name="monitorName"
      />
      <ProFormText
        initialValue={props.values?.matchValue}
        rules={[
          {
            required: true,
            message: '阈值不能为空',
          },
        ]}
        width="md"
        label="阈值"
        name="matchValue"
      />
      <ProFormText
        initialValue={props.values?.intervalTime}
        rules={[
          {
            required: true,
            message: '报警间隔时间不能为空',
          },
        ]}
        width="md"
        label="报警间隔时间(秒）"
        name="recoveryThreshold"
      />
      <ProFormText
        initialValue={props.values?.receiverKey}
        rules={[
          {
            required: true,
            message: '报警间隔时间不能为空',
          },
        ]}
        width="md"
        label="发送人KEY"
        name="receiverKey"
      />
      <ProFormRadio.Group
        initialValue={props.values?.autoRecovery || false}
        options={[
          {
            label:'是',
            value: true
          },
          {
            label:'否',
            value:false
          },
        ]}
        width="md"
        label="开始自动恢复"
        name="autoRecovery"
      />
      <ProFormText
        initialValue={props.values?.recoveryThreshold || 2}
        rules={[
          {
            required: true,
            message: '触发自动恢复阀值不能为空',
          },
        ]}
        width="md"
        label="触发自动恢复阀值"
        name="recoveryThreshold"
      />
      <ProFormTextArea
        initialValue={props.values?.description}
        width="md"
        label="描述"
        name="desc"
      />

    </ModalForm>
  );
};

export default CreateOrUpdateForm;
